<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-Clazz Tutorial</title>
        <meta name="description" content="Small Java utilities with many useful functions. Utility methods for dynamically creating class instances and inspecting constructors" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, reflection,instantiation,Java" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            // 获取用户的默认语言
            var userLang = navigator.language || navigator.userLanguage;

            // 检查是否为中文环境（包括简体和繁体）
            if (userLang.startsWith('zh') && location.pathname.indexOf('cn') == -1) {
                 confirm('欢迎！您可以改为访问中文内容。是否继续？') && location.assign('/cn');  // 如果是中文，则弹出提示
            }

            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 Home</a>
                    | ⚙️ Source:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                    | 🚀 <a href="/cn">Chinese Version</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>Small Java Utils Library</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/">Home</a>
                    </li>
                </ul>
                <h3>Common</h3>
                <ul>
                    <li><a href="/common/JsonUtil/">JsonUtil</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/ConvertBasicValue/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper/">HashHelper</a></li>
                    <li><a href="/common/MapTool/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils/">WebUtils</a></li>
                </ul>
                <h3>Date Handing</h3>
                <ul>
                    <li><a href="/date/intro/">Introduction</a></li>
                    <li><a href="/date/convert/">Date Converter</a></li>
                    <li><a href="/date/formatter/">Date Formatter</a></li>
                </ul>
                <h3>Reflection</h3>
                <ul>
                    <li><a href="/reflect/Methods/">Methods</a></li>
                    <li><a href="/reflect/Types/">Types</a></li>
                    <li><a href="/reflect/Clazz/">Clazz</a></li>
                </ul>

                <h3>IO</h3>
                <ul>
                    <li><a href="/io/FileHelper/">FileHelper</a></li>
                    <li><a href="/io/Resources/">Resources</a></li>
                    <li><a href="/io/StreamHelper/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper/">ZipHelper</a></li>
                </ul>

                <h3>HTTP Request</h3>
                <ul>
                    <li><a href="/http_request/Get">HTTP Request</a></li>
                    <li><a href="/http_request/advanced-usage/">Advanced Usage</a></li>
                    <li><a href="/http_request/Base/">Base</a></li>
                </ul>
    
               <h3>Cryptography</h3>
               <ul>
                <li><a href="/cryptography/intro/">Introduction</a></li>
                <li><a href="/cryptography/flow/">Basic Flow</a></li>
                <li><a href="/cryptography/AesCrypto/">AES/DES</a></li>
                <li><a href="/cryptography/RsaCrypto/">RSA</a></li>
               </ul>
            </menu>
            <article class="aj-text">
                <h1>Clazz</h1>
<p>The <code>Clazz</code> class provides methods for dynamically creating class instances, getting constructors, and checking if a
class has constructors with parameters. These methods facilitate efficient and convenient handling of
Java classes at runtime.</p>
<h2>Methods</h2>
<h3>1. <code>newInstance(Constructor&lt;T&gt; constructor, Object... args)</code></h3>
<p>Creates an instance of a class using the specified constructor and arguments.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>constructor</code>: The constructor to use for creating the instance.</li>
<li><code>args</code>: The arguments to pass to the constructor.</li>
</ul>
</li>
<li><strong>Returns:</strong> The created instance of type <code>T</code>.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">Constructor&lt;String&gt; constructor=String.class.getConstructor(String.class);
        String instance=Clazz.newInstance(constructor,&quot;Hello&quot;);
// instance will be &quot;Hello&quot;
</code></pre>
<h3>2. <code>hasArgsCon(Class&lt;?&gt; clz)</code></h3>
<p>Determines if a class has any constructors with parameters.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>clz</code>: The class to check.</li>
</ul>
</li>
<li><strong>Returns:</strong> <code>true</code> if the class has constructors with parameters, <code>false</code> otherwise.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">boolean hasArgsCon=Clazz.hasArgsCon(String.class);
// hasArgsCon will be true
</code></pre>
<h3>3. <code>newInstance(String className, Class&lt;T&gt; clazz)</code></h3>
<p>Creates an instance of a class by its name and casts it to the specified interface type.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>className</code>: The full name of the class.</li>
<li><code>clazz</code>: The interface type to cast to.</li>
</ul>
</li>
<li><strong>Returns:</strong> The created instance of type <code>T</code>.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">String instance=Clazz.newInstance(&quot;java.lang.String&quot;,String.class);
// instance will be an empty string
</code></pre>
<h3>4. <code>newInstance(String clzName, Object... args)</code></h3>
<p>Creates an instance of a class by its name and arguments.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>clzName</code>: The full name of the class.</li>
<li><code>args</code>: The arguments to pass to the constructor.</li>
</ul>
</li>
<li><strong>Returns:</strong> The created instance as an <code>Object</code>.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">Object instance=Clazz.newInstance(&quot;java.lang.String&quot;,&quot;Hello&quot;);
// instance will be &quot;Hello&quot;
</code></pre>
<h2>Unit Test Examples</h2>
<p>Here are some examples from the unit tests:</p>
<pre><code class="language-java">package com.ajaxjs.util.reflect;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.ajaxjs.util.reflect.Clazz.getConstructor;
import static com.ajaxjs.util.reflect.Clazz.newInstance;
import static org.junit.jupiter.api.Assertions.*;

public class TestClazz {
    @Test
    public void testGetClassByName() {
        Class&lt;?&gt; actual = Clazz.getClassByName(&quot;java.lang.String&quot;);
        assertEquals(String.class, actual);
    }

    @Test
    public void testGetClassByName_whenClassNotFound() {
        Class&lt;?&gt; actual = Clazz.getClassByName(&quot;com.example.NotFoundClass&quot;);
        assertNull(actual);
    }

    @Test
    public void testGetClassByName_whenClassFoundWithGenerics() {
        Class&lt;?&gt; actual = Clazz.getClassByName(&quot;java.util.ArrayList&quot;);
        assertEquals(ArrayList.class, actual);
    }

    @Test
    public void testGetClassByInterface() {
        Class&lt;?&gt; actual = Clazz.getClassByInterface(List.class);
        assertEquals(List.class, actual);
    }

    @Test
    public void testNewInstance() {
        assertNotNull(newInstance(TestReflectUtil.Foo.class));
        assertNotNull(newInstance(TestReflectUtil.Foo.class, &quot;a&quot;, &quot;b&quot;));
        assertNotNull(newInstance(Objects.requireNonNull(getConstructor(TestReflectUtil.Foo.class))));
        assertNotNull(newInstance(Objects.requireNonNull(getConstructor(TestReflectUtil.Foo.class, String.class, String.class)), &quot;a&quot;, &quot;b&quot;));
        assertNotNull(newInstance(&quot;com.ajaxjs.util.reflect.TestReflectUtil&quot;));
        assertNotNull(Clazz.getClassByName(&quot;com.ajaxjs.util.reflect.TestReflectUtil&quot;));

        Class&lt;?&gt;[] cs = Clazz.getDeclaredInterface(ArrayList.class);
        assertNotNull(cs);
    }
}
</code></pre>
<h2>Conclusion</h2>
<p>The <code>Clazz</code> class provides a set of useful utilities for working with Java classes through reflection. By using these
methods, you can simplify your code and handle class operations more efficiently. Remember to consult
the library's Javadoc for the most up-to-date information and additional methods.</p>

            </article>
        </div>

       <footer>
            AJ-Util, a part of <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> open source. Mail:
            frank@ajaxjs.com, visit <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>.
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>